package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.heima.article.entity.ApArticle;
import com.heima.article.service.IApArticleService;
import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 监听自媒体文章上下架
 *
 * @Name : ArticleUpDownListener
 * @Author : WuQiang
 * @Date : 2022-10-15 16:18
 * @Description : 监听自媒体文章上下架
 */
@Component
public class ArticleUpDownListener {

    @Autowired
    private IApArticleService apArticleService;

    @KafkaListener(topics = "${topic.upDownTopic}")
    public void receiveMessages(ConsumerRecord<String, String> record) {
        String value = record.value();
        if (StringUtils.isNotBlank(value)) {
            Map map = JSON.parseObject(value, Map.class);
            Long articleId = (Long) map.get("articleId");
            Boolean enable = (Boolean) map.get("enable");
            ApArticle apArticle = new ApArticle();
            apArticle.setId(articleId);
            apArticle.setIsDown(enable);
            apArticleService.updateById(apArticle);
        }
    }
}
